﻿@using BlazorComponent.JSInterop
@inject IJSRuntime jsRuntime
<div class="full-height flex-column @Class">
    @HeaderContent
    <div @ref="divRef" class="flex-height-auto @AutoClass @(Overflow?"overflow-y-auto pr-4":"")">
        @AutoHeightContent
    </div>
    @FooterContent
</div>


@code {
    [Parameter]
    public RenderFragment? HeaderContent { get; set; }

    [Parameter]
    public RenderFragment? AutoHeightContent { get; set; }

    [Parameter]
    public RenderFragment? FooterContent { get; set; }

    [Parameter]
    public string AutoClass { get; set; } = "";

    [Parameter]
    public string Class { get; set; } = "";

    [Parameter]
    public bool Overflow { get; set; }

    private ElementReference divRef;

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        await base.OnAfterRenderAsync(firstRender);

        if (firstRender)
        {
            await ScrollToTop();
        }
    }

    private async Task ScrollToTop()
    {
        if (Overflow)
        {
            await jsRuntime.ScrollTo(divRef, 0);
        }
    }
}
